package com.ym.b2b.order.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.conf.WebProviderTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.order.service.SalesDetailService;
import com.ym.b2b.order.sqlbean.EmSSaleDetail;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

@Controller
@RequestMapping("/salesDetail")
public class SalesDetailController extends BaseController {

    private Logger             logger = Logger.getLogger(getClass());
    @Autowired
    private SalesDetailService salesDetailService;

    @RequestMapping("searchSalesDetail.s")
    public String searchSalesDetail(EmSSaleDetail emSSaleDetail, Page<EmSSaleDetail> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            if (getWebProviderTokenUser(req) == null) {
                return "website/login";
            }
            page.setPageable("true");
            page.setPageUrl(req);
            emSSaleDetail.setLoginName(getWebProviderTokenUser(req).getLoginName());

            // 验证日期
            Result validRet = salesDetailService.salesDetailValidQuery(emSSaleDetail);
            if (validRet.getCode().equals("100")) {
                result.setCode(Result.CODE_100);
                result.setErrorMsg(validRet.getErrorMsg());

            } else {
                // emSSaleDetail.setQueryStart((Date) validRet.getData("queryStart"));
                salesDetailService.searchPageSalesDetail(emSSaleDetail, page);

                result.setResultPage(page);
                result.putData("svo", emSSaleDetail);
            }
            req.setAttribute("result", result);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "website/order/sale_detail_list";
    }

    /**
     * 前台供应商查销售流向
     * @Title: searchPageSalesDetail
     */
    @RequestMapping("searchPageSalesDetail.s")
    @ResponseBody
    public Result searchPageSalesDetail(EmSSaleDetail emSSaleDetail, Page<EmSSaleDetail> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebProviderTokenUser providerTokenUser = getWebProviderTokenUser(req);
            if (providerTokenUser != null) {
                emSSaleDetail.setLoginName(providerTokenUser.getLoginName());
                emSSaleDetail.setSearchStartDate(providerTokenUser.getSearchStartDate());
            }
            page = salesDetailService.searchPageSalesDetailList(emSSaleDetail, page);
            result.setResultPage(page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("searchSalesDetailExcel.s")
    @ResponseBody
    public Result searchSalesDetailExcel(EmSSaleDetail emSSaleDetail, Page<EmSSaleDetail> page, HttpServletRequest req,
                                         String loadLimit) {
        Result result = new Result();
        try {
            if (loadLimit == null || loadLimit.equals("")) {
                result.setCode(Result.CODE_100);
                result.setResult("loadLimit 参数 '数量'  不能为空!");
                return result;
            }
            page.setPageable("true");
            page.setPageUrl(req);
            page.setPageSize(Integer.parseInt(loadLimit));
            emSSaleDetail.setLoginName(getWebProviderTokenUser(req).getLoginName());

            // 验证日期
            Result validRet = salesDetailService.salesDetailValidQuery(emSSaleDetail);
            if (validRet.getCode().equals("100")) {
                result.setCode(Result.CODE_100);
                result.setErrorMsg(validRet.getErrorMsg());

            } else {
                // emSSaleDetail.setQueryStart((Date) validRet.getData("queryStart"));
                salesDetailService.searchPageSalesDetail(emSSaleDetail, page);

                result.setResultPage(page);
                result.putData("svo", emSSaleDetail);
            }
            req.setAttribute("result", result);

            ExcelUtil<EmSSaleDetail> excelUtil = new ExcelUtil<EmSSaleDetail>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", page.getRows());
            result = excelUtil.createExcel("salesDetail", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }

}
